<template>
	<div class="orrOutcupEfficiencySummary-add-update-component">
		<el-dialog
				v-if="modelValue.visible"
				v-model="modelValue.visible"
				:title="
        !modelValue.form.id
          ? `新增${modelValue.title}`
          : `修改${modelValue.title}`
      "
				width="550px"
		>
			<div style="padding-right: 50px; padding-top: 20px">
<el-form
		:model="modelValue.form"
		:rules="modelValue.rules"
		ref="addEditRef"
		:label-width="100"
>
	<el-form-item label="门店编号" prop="storeNumber">
		<el-input
				placeholder="请输入门店编号"
				v-model="modelValue.form.storeNumber"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="门店名称" prop="storeName">
		<el-input
				placeholder="请输入门店名称"
				v-model="modelValue.form.storeName"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="损耗数量" prop="lossQuantity">
		<el-input
				placeholder="请输入损耗数量"
				v-model="modelValue.form.lossQuantity"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="损耗时间（秒）" prop="lossTime">
		<el-input
				placeholder="请输入损耗时间（秒）"
				v-model="modelValue.form.lossTime"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="高峰数量" prop="highQuantity">
		<el-input
				placeholder="请输入高峰数量"
				v-model="modelValue.form.highQuantity"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="高峰时间（秒）" prop="highTime">
		<el-input
				placeholder="请输入高峰时间（秒）"
				v-model="modelValue.form.highTime"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="机器唯一ID" prop="machineUuid">
		<el-input
				placeholder="请输入机器唯一ID"
				v-model="modelValue.form.machineUuid"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="业务日期" prop="busiDate">
		<el-input
				placeholder="请输入业务日期"
				v-model="modelValue.form.busiDate"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="总数量" prop="total">
		<el-input
				placeholder="请输入总数量"
				v-model="modelValue.form.total"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="中峰数量" prop="middleQuantity">
		<el-input
				placeholder="请输入中峰数量"
				v-model="modelValue.form.middleQuantity"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="中峰时间（秒）" prop="middleTime">
		<el-input
				placeholder="请输入中峰时间（秒）"
				v-model="modelValue.form.middleTime"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="总时长（微秒）" prop="totalDuration">
		<el-input
				placeholder="请输入总时长（微秒）"
				v-model="modelValue.form.totalDuration"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="平均时长（秒）" prop="averageDuration">
		<el-input
				placeholder="请输入平均时长（秒）"
				v-model="modelValue.form.averageDuration"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="区域" prop="region">
		<el-input
				placeholder="请输入区域"
				v-model="modelValue.form.region"
				autocomplete="off"
		/>
	</el-form-item>
	<el-form-item label="门店数量" prop="storeCount">
		<el-input
				placeholder="请输入门店数量"
				v-model="modelValue.form.storeCount"
				autocomplete="off"
		/>
	</el-form-item>

</el-form>
			</div>
			<template #footer v-if="!modelValue.form.disabled">
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="ensure"> 确定 </el-button>
        </span>
			</template>
		</el-dialog>
	</div>
</template>
<script  setup lang='ts'>
	import { ref, onMounted } from "vue";
	import { ElMessage, FormInstance } from "element-plus";
	import { addOrrOutcupEfficiencySummary, updateOrrOutcupEfficiencySummary } from "@/api/orr-outcup-efficiency-summary.api";

	const props = defineProps(["modelValue"]);
	const addEditRef = ref<FormInstance>();

	/***
	 * 确认弹框
	 */
	const ensure = () => {
		let form = props.modelValue.form;
		let formData = {
																				storeNumber: form.storeNumber,
																	storeName: form.storeName,
																	lossQuantity: form.lossQuantity,
																	lossTime: form.lossTime,
																	highQuantity: form.highQuantity,
																	highTime: form.highTime,
																	machineUuid: form.machineUuid,
																	busiDate: form.busiDate,
																	total: form.total,
																	middleQuantity: form.middleQuantity,
																	middleTime: form.middleTime,
																	totalDuration: form.totalDuration,
																	averageDuration: form.averageDuration,
																	region: form.region,
																	storeCount: form.storeCount,
																																												};

		if (!addEditRef.value) return;
		addEditRef.value.validate((valid, fields) => {
			if (valid) {
				if (form.id) {
					updateOrrOutcupEfficiencySummary({
						...formData,
						id: form.id,
					})
							.then((res) => {
								cancel();
								ElMessage({
									message: props.modelValue.title + "修改成功",
									type: "success",
								});
								if (props.modelValue.refreshDataList) {
									props.modelValue.refreshDataList();
								}
							})
							.catch((err) => {
								ElMessage({
									message: err.msg || err,
									type: "error",
								});
							});
					return;
				}

				addOrrOutcupEfficiencySummary(formData)
						.then((res) => {
							cancel();
							ElMessage({
								message: props.modelValue.title + "新增成功",
								type: "success",
							});
							if (props.modelValue.refreshDataList) {
								props.modelValue.refreshDataList();
							}
						})
						.catch((err) => {
							ElMessage({
								message: err.msg || err,
								type: "error",
							});
						});
			}
		});
	};

	/***
	 * 取消弹框
	 */
	const cancel = () => {
		props.modelValue.visible = false;
	};

</script>
<style lang='scss' scoped>
</style>
